Thread: [noob] i can't even name the problem

  1. #1
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13

    [noob] i can't even name the problem

    program doesn't end loop =P

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i,temp;
    	
    	for (i=1;;i++)
    	{
    		do {
    	
    		printf("Enter the temperature for day %d (-300 to end): ",i);
    		scanf("%d",&temp);
    		
    		} while (temp=!-300);
    	}
    	
    	return 0;
    }
    also, i have to print the hottest and the coolest day. but can't figure how to do it. because day number can be any number. just give me tips. i want to do by myself. =P

  2. #2
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i,temp;
    	
    	for (i=1; i<=2;i++)
    	{
    		do {
    	
    		printf("Enter the temperature for day %d (-300 to end): ",i);
    		scanf("%d",&temp);
    		
    		} while (temp=!-300);
    	}
    	
    	return 0;
    }
    one second for the coolest and hottest day. thinking of a hint:P
    Last edited by MystWind; 03-11-2005 at 12:05 PM.
    PLay MystWind beta , within two years

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    (temp=!-300)
    You realize that that evaluates to temp = 0 right?

    I think you're looking for temp != -300 instead.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13
    well if i put i<=2 then it will stop looping. loop have to stop while temp = -300

  5. #5
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    yea , that took my attention too , but i though i might have been a C dialect ( I'm a C++ programmer ).
    PLay MystWind beta , within two years

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    well if i put i<=2 then it will stop looping. loop have to stop while temp = -300
    Get rid of the for() loop altogether then and just leave the do-while loop and increment i in the loop.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Hello,

    A few issues. If you want to use a do-while loop, you can use it without the infinite for loop. Also:
    Code:
    while (temp=!-300)
    Seems wrong, did you mean to do:
    Code:
    while (temp!=-300);
    Here's a small hint. Keep i, but don't use a for loop. Just use your do-while loop.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  8. #8
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13
    ok i removed for loop. it's unnecessary- i see...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i=1;
    	int temp;
    	
    	do {
    	
    		printf("Enter the temperature for day %d (-300 to end): ",i);
    		scanf("%d",&temp);
    		i++;
    	
    	} while (temp!=-300);
    	
    	return 0;
    }

  9. #9
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13
    but how can i calculate the hottest and the coolest day?.. i also have to take the average.. day number isn't constant.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    First i think you have to break from the while loop, otherwise it will keep looping.After this you call the AVG() function that finds the average of the temps the user inputed. For the hottest days, after your break; you could maybe sort the values to get the bigger value thus the hottest day.
    Last edited by InvariantLoop; 03-11-2005 at 01:00 PM.
    When no one helps you out. Call google();

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by knoxville
    but how can i calculate the hottest and the coolest day?.. i also have to take the average.. day number isn't constant.
    You need some extra variables to keep track of the hottest and coolest days. Probably a variable that keeps track of the hottest temperature and another to keep track of the i value associated with that temperature (the day associated with the hottest temp) and another set of variables for the coolest. Initialize the hottest temperature with INT_MIN and the coolest temperature with INT_MAX.

    Each time through the loop after you get the temperature for the day, check the temp against both the coolest and hottest temperatures. If the temp for the current day is cooler than the coolest temperature, you set the coolest temperature to be equal to the termperature for the current day and set the coolest day equal to whatever i is currently set to. If the temp for the current day if hotter than the hottest temperature, the you set the hottest temperature equal to the temperature for the current day and the hottest day equal to whatever i is currently set to.

    You also need another variable to hold the sum of the temperature values (initialized to 0). Each temperature value gets added to this sum. At the end of the program when it comes time to do the average, you simply divide this value by whatever i happens to be at the time since that is the number of days worth of temperature values we have received from the user.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13
    well i extended my code.. but miracle c gives a stupid "C:\Program Files\Miracle C\self\fusun.c: line 17: unrecognised types in comparison
    'while (temp!=-300) { counter+=1'
    aborting compile" error. =P

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	float largest,smallest,average,temp,sum;
    	int counter,day,x,y;
    	
    	smallest=0;
    	largest=0;
    	sum=0; 
    	day=1;
    	counter=0;
    	
    	printf("Enter the temperature for day %d (-300 to end): ",day);  
    	scanf("%f",&temp);
    	
    	while (temp!=-300)
    	{
    		counter+=1;
    		
    		if (temp>largest)
    		{
    			largest=temp;
    			x=day;
    		}
    		
    		if (temp<smallest)
    		{
    			smallest=temp;
    			y=day;
    		}
    	
    	++day;
    	sum+=temp;
    	
    	printf("\nEnter the temperature for day %d (-300 to end): ",day);
    	scanf("%f",&temp);
    	}
    	
    	printf("The hottest day is %d and the temperature is %f degrees celcius.\n",x,largest);
    	printf("The coolest day is %d and the temperature is %f degrees celcius.\n",y,smallest);
    	
    	average=(float)sum/counter;
    	
    	printf("The average temperature over %d days is %f degrees celcius\n",day-1,average);
    	
    	return 0;
    }

  13. #13
    Registered User knoxville's Avatar
    Join Date
    Mar 2005
    Location
    istanbul
    Posts
    13
    alright i tweaked a little. one last problem: it counts -300 as temperature. i have to use it to quit.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int day=1;
    	int cool=0,hot=0,sum=0;
    	int counter,temp,x,y;
    	float avg;
    	
    	while (temp!=-300)
    	{
    		printf("Enter the temperature for day %d (-300 to end): ",day);
    		scanf("%d",&temp);
    		
    		day++;
    		++counter;
    		
    		if (temp>hot)
    		{
    			hot=temp;
    			x=day;
    		}
    		
    		if (temp<cool)
    		{
    			cool=temp;
    			y=day;
    		}
    		
    		sum+=temp;
    	}
    	
    	avg=sum/day;
    	
    	printf("The hottest day is day %d and the temperature is %d degrees Celcius.\n",x,hot);
    	printf("The coolest day is day %d and the temperature is %d degrees Celcius.\n",y,cool);
    	printf("The average temperature over %d day(s) is %f degrees Celcius.\n",day-1,avg);
    	
    	return 0;
    }

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
       for ( ;; )
       {
          printf("Enter the temperature for day %d (-300 to end): ",day);
          scanf("%d",&temp);
          if ( temp == -300 )
          {
             break;
          }
          /* ... */
          sum+=temp;
          ++day;
       }
    Last edited by Dave_Sinkula; 03-11-2005 at 02:57 PM. Reason: Moved increment of 'day'.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    I think you're logic is a little confused, say what you want to do in english (or your native language), take it one step further into pseudo code.

    Example:

    Code:
    while temp != -300
    
    /* while temp is not equal to -300 */
    Which means for the while loop condition to fail, temp has to equal -300

    If you want to break out the loop at any given time, you might want to think about testing for an escape character or something. Otherwise, you could always base the while condition on the number of days entered or something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM